package com.sp.fresh_produce.controller;


import com.sp.fresh_produce.common.GenericReturnApiObject;
import com.sp.fresh_produce.controller.dto.VO.CartVO;
import com.sp.fresh_produce.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  购物车模块
 */
@RestController
@RequestMapping("/cart")
@Tag(name = "Cart", description = "购物车接口")
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;

    /**
     *  添加商品到购物车
     */
    @PostMapping("/add")
    @Operation(summary = "添加商品到购物车")
    public GenericReturnApiObject add(@RequestParam Integer productId, @RequestParam Integer count, jakarta.servlet.http.HttpSession session) {
        Integer userId = getCurrentUser(session).getId();
        List<CartVO> cartVOList = cartService.add(userId, productId, count);
        return GenericReturnApiObject.success(cartVOList);
    }

    /**
     *  获取购物车列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取购物车列表")
    public GenericReturnApiObject list(jakarta.servlet.http.HttpSession session) {
        // 从内部获取用户ID，防止横向越权
        Integer userId = getCurrentUser(session).getId();
        List<CartVO> cartVOList = cartService.list(userId);
        return GenericReturnApiObject.success(cartVOList);
    }

    /**
     *  更新购物车
     */
    @PutMapping("/update")
    @Operation(summary = "更新购物车")
    public GenericReturnApiObject update(@RequestParam Integer productId, @RequestParam Integer count, jakarta.servlet.http.HttpSession session) {
        Integer userId = getCurrentUser(session).getId();
        List<CartVO> cartVOList = cartService.update(userId, productId, count);
        return GenericReturnApiObject.success(cartVOList);
    }

    /**
     *  删除购物车
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除购物车")
    public GenericReturnApiObject delete(@RequestParam Integer productId, jakarta.servlet.http.HttpSession session) {
        Integer userId = getCurrentUser(session).getId();
        List<CartVO> cartVOList = cartService.delete(userId, productId);
        return GenericReturnApiObject.success(cartVOList);
    }

    /**
     *  选中购物车/反选购物车
     */
    @PutMapping("/select")
    @Operation(summary = "选中购物车/反选购物车")
    public GenericReturnApiObject select(@RequestParam Integer productId, @RequestParam Integer selected, jakarta.servlet.http.HttpSession session) {
        Integer userId = getCurrentUser(session).getId();
        List<CartVO> cartVOList = cartService.selectOrNot(userId, productId, selected);
        return GenericReturnApiObject.success(cartVOList);
    }

    /**
     *  全选中购物车/全不选中购物车
     */
    @PutMapping("/selectAll")
    @Operation(summary = "全选中购物车/全不选购物车")
    public GenericReturnApiObject selectAll(@RequestParam Integer selected, jakarta.servlet.http.HttpSession session) {
        Integer userId = getCurrentUser(session).getId();
        List<CartVO> cartVOList = cartService.selectAllOrNot(userId, selected);
        return GenericReturnApiObject.success(cartVOList);
    }
}
